package tr.edu.metu.is550.isell.activity;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import tr.edu.metu.is550.isell.R;
import tr.edu.metu.is550.isell.domain.Property;

import java.util.ArrayList;

public class ListPropertyActivity extends ListActivity {
    private MyCustomAdapter mAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<Property> properties = (ArrayList<Property>) getIntent().getExtras().get("data");
        mAdapter = new MyCustomAdapter(properties);
        setListAdapter(mAdapter);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Property property = (Property) l.getItemAtPosition(position);
        Intent myIntent = new Intent(v.getContext(), PropertyDetailActivity.class);
        myIntent.putExtra("propertyId", property.getId());
        startActivity(myIntent);
    }

    private class MyCustomAdapter extends BaseAdapter {

        private ArrayList<Property> data;

        public MyCustomAdapter(ArrayList<Property> items) {
            this.data = items;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Property getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.row, null);
            }
            Property p = data.get(position);
            if (p != null) {
                TextView tt = (TextView) v.findViewById(R.id.toptext);
                if (tt != null) {
                    tt.setText(p.getTitle());
                }
            }
            return v;
        }


    }
}
